package com.lyhlmj.oop_client.Frames.LoginAndRegister.LoginFrame;

import com.lyhlmj.oop_client.Style.Buttons.LoginFrameButton.LoginButton;
import com.lyhlmj.oop_client.Style.Buttons.LoginFrameButton.RegisterButton;
import com.lyhlmj.oop_client.Utils.GetScreenSize;

import javax.swing.*;
import java.awt.*;

public class LoginPanel extends JPanel {
    private Window owner;  //要阻塞的窗口
    private LoginButton loginButton;
    private RegisterButton registerButton;
    private JTextField username;
    private JPasswordField password;
    //  得到Frame的大小
    int width = GetScreenSize.getFitSize()[0];
    int height = GetScreenSize.getFitSize()[1];

    //    登陆面板的构造函数
    public LoginPanel(Window owner) {
        this.setOpaque(false);
        this.owner = owner;  //设置阻塞窗口为其父窗口
        this.setLayout(new GridLayout(3, 1, (int) (0.08 * height), (int) (0.08 * height)));  //设置布局形式
        this.setBorder(BorderFactory.createEmptyBorder((int) (0.4 * height), 0, 0, 0));//添加标题边框
        this.initialLoginPanel();  //调用初始化方法
    }

    //     初始化登陆面板
    private void initialLoginPanel() {
        JLabel usernameLabel = new JLabel("用  户  名   ", SwingConstants.RIGHT);
        JLabel passwordLabel = new JLabel("密  码    ", SwingConstants.RIGHT);
        usernameLabel.setFont(new Font("隶书", 1, 40));
        passwordLabel.setFont(new Font("隶书", 1, 40));
        username = new JTextField(20);
        password = new JPasswordField(20);
        loginButton = new LoginButton("登  陆", owner);
        registerButton = new RegisterButton("注  册", owner);
//        注意控件添加的顺序
        this.add(usernameLabel);
        this.add(username);
        this.add(passwordLabel);
        this.add(password);
        this.add(loginButton);
        this.add(registerButton);
    }

    //    添加按钮的点击事件
    public void setButtonFunction() {
        loginButton.addActionListener(e -> {  //添加按钮单击事件（lambda表达式形式
            loginButton.doLogin(username.getText(), password.getText());
        });
        registerButton.addActionListener(e -> {  //添加按钮单击事件（lambda表达式形式
            registerButton.doRegister();
        });
    }

}
